package cn.git.agent;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/** 
 * @description: 插件配置文件,转义git-plugins.def文件到PluginDefine实例
 * @program: bank-credit-sy
 * @author: lixuchun
 * @create: 2024-12-27
 */
public enum PluginCfg {
    INSTANCE;

    /**
     * 存放所有插件.def文件中，构造出来的 PluginDefine 实例
     */
    private List<PluginDefine> pluginClassList = new ArrayList<PluginDefine>();

    /**
     * 加载插件配置文件,转换为PluginDefine实例
     *
     * @param input
     * @throws IOException
     */
    void load(InputStream input) throws IOException {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            String pluginDefine;
            while ((pluginDefine = reader.readLine()) != null) {
                try {
                    if (pluginDefine.trim().length() == 0 || pluginDefine.startsWith("#")) {
                        continue;
                    }
                    PluginDefine plugin = PluginDefine.build(pluginDefine);
                    pluginClassList.add(plugin);
                } catch (Exception e) {
                    System.out.println("load plugin error: " + pluginDefine);
                }
            }
        } finally {
            input.close();
        }
    }

    /**
     * 获取所有插件
     * @return
     */
    public List<PluginDefine> getPluginClassList() {
        return pluginClassList;
    }

}
